import { defineStore } from "pinia";
import { worksList, deleteTasks } from "../../api";
import { ElMessage } from 'element-plus';
import { useRouter } from "vue-router";

export const useTaskManagementStore = defineStore('taskManagement', {
    state: () => ({
        adminWorksList: [],//当前显示的数据
        allTaskList:[],//全部的任务
        adminWorksListCopy: [],//配合分页，做拆分
        isSomeIngChecked: false,
        showMask: true,
        currentPage: 1,
        pageSize: 10,
        searchButtonDisabled: false,
        resetButtonDisabled: false,
        pageButtonDisabled: false,
    }),
    getters: {
        pageNumber(state) {
            return { 
                pageStart: (state.currentPage - 1) * state.pageSize,
                pageEnd: state.pageSize
            };
        }
    },
    actions: {
        async handleApiError(res) {
            if (res.data.code === 401 || res.data.code === 500) {
                ElMessage.error(res.data.msg || "服务器错误");
                if (res.data.msg === "非法token") {
                    const router = useRouter();
                    router.push("/login");
                }
            }
        },
        async deleteTasks(data) {
            this.showMask = true;
            try {
                const res = await deleteTasks(JSON.stringify(data));
                if(res.data.code!=401&&res.data.code!=500&&res.data.msg!="认证失败，请尝试重新登录！"){
                    const taskIds = data.taskIds;
                    this.adminWorksList = this.adminWorksList.filter(val => !taskIds.includes(val.id));
                    this.adminWorksListCopy = this.adminWorksListCopy.filter(val => !taskIds.includes(val.id));
                    ElMessage.success('已删除');                    
                }else{
                    ElMessage.error(res.data.msg);
                }
              
            } catch (error) {
                console.error(error);
                ElMessage.error('删除任务时出错');
            } finally {
                let timer = setTimeout(()=>{
                    this.showMask = false;
                    clearTimeout(timer)
                },1000)
           
            }
        },
        async getWorksList() {
            this.showMask = true;
            try {
                const res = await worksList();                
                if (res.data.code == 200 && res.data.data) {
                    const arr = res.data.data.map(val => ({ ...val, checked: false }));
                    const { pageStart, pageEnd } = this.pageNumber;
                    this.adminWorksList = arr.slice(pageStart, pageStart + pageEnd);
                  
                    this.adminWorksListCopy = [...arr];
                    this.allTaskList=[...arr]
                } else {
                    this.handleApiError(res);
                }
            } catch (error) {
                console.error(error);
                ElMessage.error('获取任务列表时出错');
            } finally {
                       let timer = setTimeout(()=>{
                this.showMask = false;
                           clearTimeout(timer)
                },1000)
            }
        },
        clearPageData(){
            this.currentPage=1;
            this.pageSize=10;
        },
    }
});